import {CardsType, King, poker} from "./types";
import {createCardGroup} from "./fnns";
interface playcard {
    playcard1: classCards
    playcard2: classCards
    playcard3: classCards
    win: classCards
}
export class classCards {
    CardGroup: CardsType = []

    constructor(Cards?: CardsType) {
        if (Cards) {
            this.CardGroup = Cards
        } else {
            this.init()
            this.shuffle()
        }
    }
    // 初始化
    private init() {
        this.CardGroup = createCardGroup()
    }
    //发牌机
    licensing(): playcard {
        const arr: CardsType[] = [[], [], [], []]
        while (this.CardGroup.length > 3) {
            arr[0].push(this.CardGroup.shift() as King | poker)
            arr[1].push(this.CardGroup.shift() as King | poker)
            arr[2].push(this.CardGroup.shift() as King | poker)
        }
        arr[3] = this.CardGroup
        return {
            playcard1: new classCards(arr[0]),
            playcard2: new classCards(arr[1]),
            playcard3: new classCards(arr[2]),
            win: new classCards(arr[3]),
        }
    }
    //洗牌
    private shuffle() {
        for (let i = 0; i < this.CardGroup.length; i++) {
            let newindex = classCards.random(i, this.CardGroup.length)
            let item = this.CardGroup[i]
            this.CardGroup[i] = this.CardGroup[newindex]
            this.CardGroup[newindex] = item
        }
    }
    //随机数
    private static random(min: number, max: number) {
        return Math.floor(Math.random() * (max - min)) + min;
    }
    //打印卡牌
    print() {
        let str: string = ''
        this.CardGroup.forEach((Card, i) => {
            str += Card.valueOf() + '\t'
            if ((i + 1) % 4 === 0) {
                str += '\n'
            }
        })
        console.log(str)
    }
}
